Completed
Push — master ( f48291...a65f2c )
by greg
74:59
created

)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 5
rs 9.4285
nop 1
1
var chai = require('chai');
2
var path = require('path');
3
4
var config = require('../src/cli').config
5
config.set({root: __dirname + '/fixtures'})
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
6
7
var cmsData = require('../src/cli').cmsData
8
var Manager = require('../src/cli').Manager;
9
var fse = require('fs-extra');
10
11
describe('Request', function() {
12
  before( function(done) {
13
    Manager.instance.init()
14
      .then(function () {
15
        Manager.instance._whereKeys = ['title', 'priority', 'abe_meta', 'articles']
16
        Manager.instance.updateList()
17
18
        this.fixture = {
19
          tag: fse.readFileSync(__dirname + '/fixtures/templates/article.html', 'utf8'),
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
20
          jsonArticle: fse.readJsonSync(__dirname + '/fixtures/data/article-1.json'),
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
21
          jsonHomepage: fse.readJsonSync(__dirname + '/fixtures/data/homepage-1.json')
0 ignored issues
show
Compatibility introduced by
Consider using the path module for constructing paths since they are otherwise not cross-OS compatible.
Loading history...
22
        }
23
        done()
24
        
25
      }.bind(this))
26
  });
27
28
  /**
29
   * cmsData.sql.executeQuery
30
   * 
31
   */
32
  it('cmsData.sql.executeQuery()', function(done) {
33
    try {
34
      var match = 'select * from ../'
35
      var jsonPage = {}
0 ignored issues
show
Unused Code introduced by
The variable jsonPage seems to be never used. Consider removing it.
Loading history...
36
      var res = cmsData.sql.handleSqlRequest(match, {})
37
38
      chai.assert.equal(res.string, 'select ["*"] from ["___abe_dot______abe_dot______abe___"] ', 'select not well formatted')
39
      done();
40
    } catch (x) {
41
      done(x);
42
    }
43
  });
44
45
  /**
46
   * cmsData.sql.executeFromClause
47
   * 
48
   */
49
  it('cmsData.sql.executeFromClause()', function() {
50
    var res = cmsData.sql.executeFromClause(['/'], ['/'])
51
    chai.expect(res).to.have.length(2);
52
  });
53
54
  it('cmsData.sql.executeWhereClause() =', function() {
55
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`=`article`', {})
56
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
57
    chai.expect(res, '`abe_meta.template`=`article`').to.have.length(1);
58
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
59
60
    request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`=`{{abe_meta.template}}`', this.fixture.jsonHomepage)
61
    res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, this.fixture.jsonHomepage)
62
    chai.expect(res, '`abe_meta.template`=`{{abe_meta.template}}`').to.have.length(1);
63
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
64
65
    request = cmsData.sql.handleSqlRequest('select title from ./ where `{{abe_meta.template}}`=`{{abe_meta.template}}`', this.fixture.jsonHomepage)
66
    res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, this.fixture.jsonHomepage)
67
    chai.expect(res, '`{{abe_meta.template}}`=`{{abe_meta.template}}`').to.have.length(1);
68
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
69
  });
70
  it('cmsData.sql.executeWhereClause() !=', function() {
71
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`!=`homepage`', {})
72
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
73
    chai.expect(res, '`abe_meta.template`!=`homepage`').to.have.length(1);
74
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
75
  });
76
  it('cmsData.sql.executeWhereClause() >', function() {
77
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `priority`>`1`', {})
78
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
79
    chai.expect(res, '`priority`>`1`').to.have.length(1);
80
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
81
  });
82
  it('cmsData.sql.executeWhereClause() >=', function() {
83
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `priority`>=`1`', {})
84
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
85
    chai.expect(res, '`priority`>=`1`').to.have.length(2);
86
  });
87
  it('cmsData.sql.executeWhereClause() <', function() {
88
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `priority`<`1`', {})
89
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
90
    chai.expect(res, '`priority`<`1`').to.have.length(0);
91
  });
92
  it('cmsData.sql.executeWhereClause() <=', function() {
93
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `priority`<=`1`', {})
94
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
95
    chai.expect(res, ' `priority`<=`1`').to.have.length(1);
96
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
97
  });
98
  it('cmsData.sql.executeWhereClause() LIKE', function() {
99
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` LIKE `home`', {})
100
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
101
    chai.expect(res, '`abe_meta.template` LIKE `home`').to.have.length(1);
102
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
103
  });
104
  it('cmsData.sql.executeWhereClause() NOT LIKE', function() {
105
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` NOT LIKE `home`', {})
106
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
107
    chai.expect(res, '`abe_meta.template` NOT LIKE `home`').to.have.length(1);
108
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
109
  });
110
  it('cmsData.sql.executeWhereClause() AND', function() {
111
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`=`homepage` AND title=`homepage`', {})
112
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
113
    chai.expect(res, '`abe_meta.template`=`homepage` AND title=`homepage`').to.have.length(1);
114
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
115
  });
116
  it('cmsData.sql.executeWhereClause() OR', function() {
117
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template`=`homepage` OR `abe_meta.template`=`article`', {})
118
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
119
    chai.expect(res, '`abe_meta.template`=`homepage` OR `abe_meta.template`=`article`').to.have.length(2);
120
  });
121
  it('cmsData.sql.executeWhereClause() IN', function() {
122
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` IN (`homepage`,`test`)', {})
123
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
124
    chai.expect(res, '`abe_meta.template` IN (`homepage`,`test`)').to.have.length(1);
125
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
126
127
    request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` IN (`{{articles}}`)', this.fixture.jsonHomepage)
128
    res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, this.fixture.jsonHomepage)
129
    chai.expect(res, '`abe_meta.template` IN (`{{articles}}`').to.have.length(1);
130
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
131
  });
132
  it('cmsData.sql.executeWhereClause() NOT IN', function() {
133
    var request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` NOT IN (`homepage`,`test`)', {})
134
    var res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, {})
135
    chai.expect(res, '`abe_meta.template` NOT IN (`homepage`,`test`)').to.have.length(1);
136
    chai.assert.equal(res[0].title, 'article', 'expected select to find article but found ' + res[0].title);
137
138
    request = cmsData.sql.handleSqlRequest('select title from ./ where `abe_meta.template` NOT IN (`{{articles}}`)', this.fixture.jsonHomepage)
139
    res = cmsData.sql.executeWhereClause(Manager.instance.getList(), request.where, request.limit, request.columns, this.fixture.jsonHomepage)
140
    chai.expect(res, '`abe_meta.template` NOT IN (`{{articles}}`)').to.have.length(1);
141
    chai.assert.equal(res[0].title, 'homepage', 'expected select to find homepage but found ' + res[0].title);
142
  });
143
144
  /**
145
   * cmsData.sql.whereLike
146
   * 
147
   */
148
  it('cmsData.sql.getSourceType()', function() {
149
    chai.expect(cmsData.sql.getSourceType('http://google.com')).to.equal('url');
150
    chai.expect(cmsData.sql.getSourceType('select * from test')).to.equal('request');
151
    chai.expect(cmsData.sql.getSourceType('{"test":"test"}')).to.equal('value');
152
    chai.expect(cmsData.sql.getSourceType('references.json')).to.equal('file');
153
    chai.expect(cmsData.sql.getSourceType('test')).to.equal('other');
154
  });
155
156
  /**
157
   * cmsData.source.requestList
158
   * 
159
   */
160
  it('cmsData.source.requestList()', function(done) {
161
    var matches = cmsData.regex.getTagAbeTypeRequest(this.fixture.tag)
162
163
    chai.expect(matches[0][0]).to.not.be.null
0 ignored issues
show
introduced by
The result of the property access to chai.expect(matches.0.0).to.not.be.null is not used.
Loading history...
164
165
    var attributes = cmsData.attributes.getAll(matches[0][0], {})
166
    chai.expect(matches[0][0]).to.not.be.null
0 ignored issues
show
introduced by
The result of the property access to chai.expect(matches.0.0).to.not.be.null is not used.
Loading history...
167
168
    var jsonPage = {}
169
    cmsData.source.requestList(attributes, '', matches[0][0], jsonPage)
170
      .then(function () {
171
        chai.expect(jsonPage.abe_source).to.not.be.undefined
0 ignored issues
show
introduced by
The result of the property access to chai.expect(jsonPage.abe...ce).to.not.be.undefined is not used.
Loading history...
172
        done()
173
      })
174
  });
175
});
176